package com.hxd.otc.module.my.activity;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.module.my.adapter.VIPServiceAdapter;
import com.hxd.otc.module.my.bean.ApplyRequestBean;
import com.hxd.otc.module.my.contract.VIPServiceContract;
import com.hxd.otc.module.my.presenter.VIPServicePresenter;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;

public class VIPServiceActivity extends BaseActivity<VIPServicePresenter> implements VIPServiceContract.View {

    @BindView(R.id.et_name)
    EditText etName;

    @BindView(R.id.et_phone_number)
    EditText etPhoneNumber;

    @BindView(R.id.et_other_contanct)
    EditText etOtherContanct;

    @BindView(R.id.et_referrer)
    EditText etReferrer;

    @BindView(R.id.rb_selling)
    CheckBox rbSelling;

    @BindView(R.id.rb_buy)
    CheckBox rbBuy;

    @BindView(R.id.rc_expected_currency)
    RecyclerView rcExpectedCurrency;

    @BindView(R.id.rc_deal_currency)
    RecyclerView rcDealCurrency;

    @BindView(R.id.rc_deal_type)
    RecyclerView rcDealType;


    VIPServiceAdapter expectedAdapter;
    VIPServiceAdapter currencyAdapter;
    VIPServiceAdapter dealTypeAdapter;

    String[] expectedArray;
    String[] currencyArray;
    String[] dealTypeArray;

    String youNeesBuy, youNeedSell;

    ApplyRequestBean bean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_vipservice;
    }

    @Override
    protected void initView() {
        bean = new ApplyRequestBean();
        rcExpectedCurrency.setLayoutManager(new GridLayoutManager(this, 4));
        rcDealCurrency.setLayoutManager(new GridLayoutManager(this, 4));
        rcDealType.setLayoutManager(new GridLayoutManager(this, 3));

        expectedAdapter = new VIPServiceAdapter();
        currencyAdapter = new VIPServiceAdapter();
        dealTypeAdapter = new VIPServiceAdapter();

        rcExpectedCurrency.setAdapter(expectedAdapter);
        rcDealCurrency.setAdapter(currencyAdapter);
        rcDealType.setAdapter(dealTypeAdapter);

        expectedArray = getResources().getStringArray(R.array.vip_expected_currency);
        currencyArray = getResources().getStringArray(R.array.vip_deal_currency);
        dealTypeArray = getResources().getStringArray(R.array.vip_deal_type);

        expectedAdapter.setNewData(Arrays.asList(expectedArray));
        currencyAdapter.setNewData(Arrays.asList(currencyArray));
        dealTypeAdapter.setNewData(Arrays.asList(dealTypeArray));

        rbBuy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    youNeesBuy = rbBuy.getText().toString();
                } else {
                    youNeesBuy = "";
                }
            }
        });
        rbSelling.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    youNeedSell = rbSelling.getText().toString();
                } else {
                    youNeedSell = "";
                }
            }
        });

        expectedAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            }
        });
        currencyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        dealTypeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
    }

    @OnClick({R.id.tv_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_submit:
                if (TextUtils.isEmpty(etName.getText().toString())) {
                    showToast(getString(R.string.name_not_null_hint));
                    return;
                }

                if (TextUtils.isEmpty(etPhoneNumber.getText().toString())) {
                    showToast(getString(R.string.phone_number_not_null));
                    return;
                }

                if (TextUtils.isEmpty(youNeedSell) && TextUtils.isEmpty(youNeesBuy)) {
                    showToast(getString(R.string.your_need_hint));
                    return;
                }

                if (expectedAdapter.getStrings().size() <= 0) {
                    showToast(getString(R.string.enter_expected_transaction_currency_hint));
                    return;
                }
                if (currencyAdapter.getStrings().size() <= 0) {
                    showToast(getString(R.string.enter_transaction_currency_hint));
                    return;
                }
                if (dealTypeAdapter.getStrings().size() <= 0) {
                    showToast(getString(R.string.enter_transaction_type_hint));
                    return;
                }


                if (!TextUtils.isEmpty(youNeesBuy)) {
                    bean.setRequest(youNeesBuy);
                }
                if (!TextUtils.isEmpty(youNeedSell)) {
                    if (!TextUtils.isEmpty(bean.getRequest())) {
                        bean.setRequest(bean.getRequest() + "," + youNeedSell);
                    } else {
                        bean.setRequest(youNeedSell);
                    }
                }
                String expectedStr = "";
                String currencyStr = "";
                String dealTypedStr = "";

                for (int i = 0; i < expectedAdapter.getStrings().size(); i++) {
                    if (TextUtils.isEmpty(expectedStr)) {
                        expectedStr = expectedAdapter.getStrings().get(i);
                    } else {
                        expectedStr = expectedStr + "," + expectedAdapter.getStrings().get(i);
                    }
                }
                for (int i = 0; i < currencyAdapter.getStrings().size(); i++) {
                    if (TextUtils.isEmpty(currencyStr)) {
                        currencyStr = currencyAdapter.getStrings().get(i);
                    } else {
                        currencyStr = currencyStr + "," + currencyAdapter.getStrings().get(i);
                    }
                }
                for (int i = 0; i < dealTypeAdapter.getStrings().size(); i++) {
                    if (TextUtils.isEmpty(dealTypedStr)) {
                        dealTypedStr = dealTypeAdapter.getStrings().get(i);
                    } else {
                        dealTypedStr = dealTypedStr + "," + dealTypeAdapter.getStrings().get(i);
                    }
                }

                bean.setCoin(expectedStr);
                bean.setLegalCoin(currencyStr);
                bean.setPayWay(dealTypedStr);
                bean.setContactName(etName.getText().toString());
                bean.setContactPhone(etPhoneNumber.getText().toString());
                bean.setOtherContact(etOtherContanct.getText().toString());
                bean.setRecomAccount(etReferrer.getText().toString());
                bean.setType(2);
                mPresenter.onSubmit(bean);
                break;
        }
    }

    @Override
    protected VIPServicePresenter createPresenter() {
        return new VIPServicePresenter(this);
    }


    @Override
    public void onSubmitSuccess() {
        showToast(getString(R.string.sunmit_succed));
        finish();
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
    }
}
